package com.taotao.portal.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.result.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.pojo.TbContent;
import com.taotao.portal.pojo.AdItem;
import com.taotao.portal.service.ContentService;
@Service
public class ContentServiceImpl implements ContentService {
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_INDEX_AD_URL}")
	private String REST_INDEX_AD_URL;
	
	@Override
	public String getContentList() {
		String result= HttpClientUtil.doGet(REST_BASE_URL+REST_INDEX_AD_URL);
		try {
			   TaotaoResult taotaoResult=TaotaoResult.formatToList(result, TbContent.class);
			   List<TbContent> resultList= (List<TbContent>) taotaoResult.getData();
			   List<AdItem> adItems=new ArrayList<>();
			   for(TbContent content:resultList){
				   AdItem adItem=new AdItem();
				   adItem.setSrc(content.getPic());
				   adItem.setHeight(240);
				   adItem.setWidth(670);
				   adItem.setSrcB(content.getPic2());
				   adItem.setHeightB(240);
				   adItem.setHeightB(550);
				   adItem.setHref(content.getUrl());
				   adItem.setAlt(content.getSubTitle());
				   adItems.add(adItem);
			   }
				return JsonUtils.objectToJson(adItems);
		} catch (Exception e) {
			e.getStackTrace();
		}
		
	  return null;
	}

}
